<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="老人姓名" prop="elderName">
              <el-input v-model="queryParams.elderName" placeholder="请输入老人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="费用来源" prop="feeSource">
              <el-select v-model="queryParams.feeSource" placeholder="请选择费用来源" clearable >
                <el-option v-for="dict in sys_fee_source" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport">导出详细</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Back" @click="goBack">返回汇总</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="elderOutstandingFeesList">
        <el-table-column label="记录ID" align="center" prop="recordId" />
        <el-table-column label="老人姓名" align="center" prop="elderName" />
        <el-table-column label="费用来源" align="center" prop="feeSource">
          <template #default="scope">
            <dict-tag :options="sys_fee_source" :value="scope.row.feeSource"/>
          </template>
        </el-table-column>
        <el-table-column label="费用描述" align="center" prop="feeDescription" v-if="false" />
        <el-table-column label="计费日期" align="center" prop="billDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="应付日期" align="center" prop="dueDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.dueDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="费用总额" align="center" prop="totalAmount" />
        <el-table-column label="已付金额" align="center" prop="paidAmount" />
        <el-table-column label="未付金额" align="center" prop="outstandingAmount" />
        <el-table-column label="支付状态" align="center" prop="paymentStatus">
          <template #default="scope">
            <dict-tag :options="payment_status" :value="scope.row.paymentStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="最后付款日期" align="center" prop="lastPaymentDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.lastPaymentDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付款截止日期" align="center" prop="paymentDeadline" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.paymentDeadline, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否逾期" align="center" prop="isOverdue" />
        <el-table-column label="逾期天数" align="center" prop="overdueDays" />
        <el-table-column label="催款次数" align="center" prop="reminderCount" />
        <el-table-column label="最后催款日期" align="center" prop="lastReminderDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.lastReminderDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ElderOutstandingFeesDetail" lang="ts">
import { listElderOutstandingFeesDetail, exportElderOutstandingFeesDetail } from '@/api/nursing/elderOutstandingFees';
import { ElderOutstandingFeesVO, ElderOutstandingFeesQuery } from '@/api/nursing/elderOutstandingFees/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_fee_source, payment_status } = toRefs<any>(proxy?.useDict('sys_fee_source', 'payment_status'));
const route = useRoute();

const elderOutstandingFeesList = ref<ElderOutstandingFeesVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = reactive<ElderOutstandingFeesQuery>({
  pageNum: 1,
  pageSize: 10,
  elderName: undefined,
  feeSource: undefined,
  params: {}
});

/** 查询欠费记录详细列表 */
const getList = async () => {
  loading.value = true;
  const res = await listElderOutstandingFeesDetail(queryParams);
  elderOutstandingFeesList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('nursing/elderOutstandingFees/exportDetail', {
    ...queryParams
  }, `elderOutstandingFeesDetail_${new Date().getTime()}.xlsx`)
}

/** 返回汇总页面 */
const goBack = () => {
  window.close();
}

onMounted(() => {
  // 从URL参数中获取查询条件
  if (route.query.elderName) {
    queryParams.elderName = route.query.elderName as string;
  }
  if (route.query.feeSource) {
    queryParams.feeSource = route.query.feeSource as string;
  }
  getList();
});
</script>
